package com.sunfeng.elasticsearch.document.search;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @Author : 清风冷影
 * @Description:
 * @Date : 2021/11/4  21:00
 */
@SpringBootTest
public class ESTest_Doc_Query_SpringBoot {

    @Test
    void contextLoads() throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.139.187", 9200, "http"))
        );

        // aggregationsTest(client);
        client.close();
    }





    private void searchAll(RestHighLevelClient client) throws IOException {

        //1、查询所有
        SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        request.source(query);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);



    }

    private void searchAge(RestHighLevelClient client) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30));
        request.source(query);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void searchForPage(RestHighLevelClient client) throws IOException {
        // 3、分页查询
        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        sourceBuilder.from(0);
        sourceBuilder.size(2);
        request.source(sourceBuilder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    private void searchSort(RestHighLevelClient client) throws IOException {
        // 4、查询排序
        SearchRequest searchRequest = new SearchRequest();

        searchRequest.indices("user");
        // 查询全部数据
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        sourceBuilder.sort("age", SortOrder.DESC);
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }

    }

    // 5、过滤字段
    private void excludeSearch(RestHighLevelClient client) throws IOException {


        SearchRequest searchRequest = new SearchRequest();

        searchRequest.indices("user");
        // 查询全部数据
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

        String[] includes = {};
        String[] excludes = {"age"};
        sourceBuilder.fetchSource(includes, excludes);

        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }

    }

    private void boolQuery(RestHighLevelClient client) throws IOException {

        SearchRequest searchRequest = new SearchRequest();

        searchRequest.indices("user");
        // 查询全部数据
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.should(QueryBuilders.matchQuery("age", 30));
        boolQuery.should(QueryBuilders.matchQuery("age", 40));
        sourceBuilder.query(boolQuery);
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }
    // match    全文检索字段用这个 进行分词查询   keyword  (进行完全匹配   整个的查询内容和字段内容完全匹配 )
    // match_phrase   进行短语匹配
    // term  是进行分词匹配  text 字段不建议使用这个   精确值的匹配  (age)

    private void test(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest();

        searchRequest.indices("user");
        // 分词匹配
        // SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchQuery("name","lisi"));
        // 短语匹配
        //     SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchPhraseQuery("name", "wangwu"));
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.termQuery("age","30"));
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }

    }

    private  void  aggregationsTest(RestHighLevelClient client) throws IOException{
                SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder();

        MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        builder.aggregation(aggregationBuilder);
        request.source(builder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());
        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }

    }

}
